package com.iflytek.read;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.iflytek.read.bean.FloatWindowBean;
import com.iflytek.read.util.LogUtil;
import com.iflytek.read.window.EasyWindow;

public class ReadTestActivity extends AppCompatActivity {
    private MusicPlayerService musicPlayerService;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.read_test_activity);

        findViewById(R.id.btn_bind_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindService();
            }
        });

        findViewById(R.id.btn_play_voice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = ReadTestActivity.this.getFilesDir() + "/test.wav";
                playVoice(path);
            }
        });

        findViewById(R.id.btn_pause_voice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pauseVoice();
            }
        });

        findViewById(R.id.btn_resume_voice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resumeVoice();
            }
        });

        findViewById(R.id.btn_stop_voice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopVoice();
            }
        });

        findViewById(R.id.btn_test_pop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EasyWindow.with(ReadTestActivity.this)
                        .setContentView(R.layout.test_float_window)  // 设置悬浮窗布局
                        .setWidth(WindowManager.LayoutParams.WRAP_CONTENT)  // 设置宽度
                        .setHeight(WindowManager.LayoutParams.WRAP_CONTENT) // 设置高度
                        .setGravity(Gravity.END | Gravity.BOTTOM)  // 右下角显示
                        .setOffSetX(20)              // 水平偏移
                        .setOffSetY(20)              // 垂直偏移
                        .setTag("global_window")     // 设置标记
                        .setOutsideTouchable(true)   // 设置外部可触摸
                        .setDraggable()              // 启用拖拽
                        .setOnWindowLifecycle(new EasyWindow.OnWindowLifecycle() {
                            @Override
                            public void onWindowShow(EasyWindow<?> easyWindow) {
                                LogUtil.d("TAG", "弹窗已经显示");
                            }

                            @Override
                            public void onWindowCancel(EasyWindow<?> easyWindow) {
                                LogUtil.d("TAG", "弹窗已经取消显示");
                            }
                        })
                        .show();                     // 显示悬浮窗
            }
        });

        findViewById(R.id.btn_test_dismiss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EasyWindow.cancelWindowByTag("global_window");
//                EasyWindow.recycleAllWindow();
            }
        });
    }

    // 1. 创建 Service 连接
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MusicPlayerBinder binder = (MusicPlayerBinder) service;
            musicPlayerService = binder.getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicPlayerService = null;
        }
    };

    // 2. 绑定服务
    private void bindService() {
        Intent intent = new Intent(this, MusicPlayerService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    // 3. 播放音频
    private void playVoice(String audioFilePath) {
        // 创建 FloatWindowBean 对象，设置音频信息
        FloatWindowBean bean = new FloatWindowBean();
        //如果是
        bean.setFilePath(audioFilePath);  // 设置音频文件路径
        bean.setSpeed("50");  // 设置播放速度（可选）
        // 调用服务播放音频
        // 如果使用 Constant.PLAYER_ID_1, 那么传 FilePath 否则传 url2
        musicPlayerService.playAudio(bean, Constant.PLAYER_ID_1);
    }

    // 4. 控制播放
    private void pauseVoice() {
        // 暂停
        musicPlayerService.pauseAudio(Constant.PLAYER_ID_1);
    }

    private void resumeVoice() {
        // 恢复
        musicPlayerService.resumeAudio(Constant.PLAYER_ID_1);
    }

    private void stopVoice() {
        // 停止
        musicPlayerService.stopAudio(Constant.PLAYER_ID_1);
    }

    // 5. 在 Activity 销毁时解绑服务
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (serviceConnection != null) {
            unbindService(serviceConnection);
        }
    }
}
